﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace IFPRJ.EF.Components.SkinEgine
{
    /// <summary>
    /// 支持换肤的视图管理器视图引擎。会根据cookie来自动选择相应的视图引擎。
    /// </summary>
    public class SkinViewEngine : IViewEngine
    {
        // 可选视图引擎容器
        private static readonly IDictionary<string, IViewEngine> _engines = new Dictionary<string, IViewEngine>();
        public IDictionary<string, IViewEngine> Engines
        {
            get { return _engines; }
        }

        // 构造函数
        public SkinViewEngine(string[] skins)
        {
            Engines.Clear();
            if (skins != null)
                foreach (string skin in skins)
                    Engines.Add(skin, new SkinWebFormViewEngine(skin));
        }

        // 根据皮肤名称获取对应的 ViewEngine
        private IViewEngine Find(ControllerContext context)
        {
            string skin = context.HttpContext.GetSkinName();
            if (!_engines.ContainsKey(skin))
                throw new Exception(String.Format("模板{0}未找到.", skin));
            return _engines[skin];
        }


        #region IViewEngine 成员
        public void ReleaseView(ControllerContext context, IView view)
        {
            Find(context).ReleaseView(context, view);
        }

        public ViewEngineResult FindPartialView(ControllerContext context, string partialViewName, bool useCache)
        {
            return Find(context).FindPartialView(context, partialViewName, useCache);
        }

        public ViewEngineResult FindView(ControllerContext context, string viewName, string masterName, bool useCache)
        {
            return Find(context).FindView(context, viewName, masterName, useCache);
        }
        #endregion
    }
}
